package com.come4loves.game.module.shop.facade.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.come4loves.core.result.TResult;
import com.come4loves.core.utils.DateUtils;
import com.come4loves.game.constent.GameStatusCodeConstant;
import com.come4loves.game.dataconfig.model.RandomShopConfig;
import com.come4loves.game.dataconfig.service.ShopService;
import com.come4loves.game.dataconfig.service.SystemService;
import com.come4loves.game.dbproxy.entity.Arena;
import com.come4loves.game.dbproxy.entity.Shop;
import com.come4loves.game.module.arena.dao.ArenaDao;
import com.come4loves.game.module.card.dao.CardDao;
import com.come4loves.game.module.card.facade.CardFacade;
import com.come4loves.game.module.card.model.CardModel;
import com.come4loves.game.module.shop.dao.ShopDao;
import com.come4loves.game.module.shop.facade.ShopFacade;

public class ShopFacadeImpl implements ShopFacade {

	@Autowired
	ShopDao shopdao;

	@Autowired
	ArenaDao arenaDao;
	
	@Autowired
	CardDao cardDao;
	
	@Autowired
	CardFacade cardFacade;
	
	@Override
	public TResult<List<CardModel>> refreshCard(long actorId) {
		Shop shop = shopdao.get(actorId);
		Arena arena = arenaDao.getArena(actorId);
		int time = SystemService.systemConfig.getCardRefreshTime();
		boolean isOverTime = DateUtils.beyondTheTime(shop.refreshTime, time);
		List<CardModel> list = new ArrayList<>();
		if (shop.isNewEntity() || isOverTime) {
			Collection<RandomShopConfig> randomConfigByArena = ShopService.randomConfigByArena(4, arena.level, false);
			for (RandomShopConfig randomShopConfig : randomConfigByArena) {
				CardModel cardModel = cardDao.getCardModel(actorId, randomShopConfig.geCardType(), randomShopConfig.getId());
				CardModel model = CardModel.valueOf(cardModel.cfgId, 1, randomShopConfig.geCardType(), cardModel.count, shop.buyCardCountMap.get(cardModel.cfgId));
				list.add(model);
			}
			return TResult.sucess(list);
		}
		return TResult.valueOf(GameStatusCodeConstant.SHOP_REFRESH_TIME_ERROR);
	}

}
